﻿using House.Server.Dto;
using House.Server.Service;
using House.Server.WebApi.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Results;
using System.Web.Http.Routing;

namespace House.Server.WebApi.Controllers
{
    public class EntranceController : ApiController
    {
        #region  登录权限部分 API接口

        /// <summary>
        /// 验证码
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/GetCodeImg_Login")]
        public dynamic GetCodeImg()
        {
            return LoginViewModel.LoginCodeImge();
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="name">账号</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        public JsonResult<Common.LoginCommon> GetStaffLogin(string name, string pwd)
        {
            House.Server.WebApi.Common.LoginCommon login = new Common.LoginCommon();
            login.LoginState = StaffViewModel.GetStaffLogin(name, pwd);
            List<Staff> staff = StaffViewModel.StaffSelect();
            if (login.LoginState==0)
            {
                login.LoginKeyId = 0;
                return Json<Common.LoginCommon>(login);
            }
            else if (login.LoginState == 1)
            {
                login.LoginKeyId = 0;
                return Json<Common.LoginCommon>(login);
            }
            else
            {
                login.LoginKeyId = login.LoginKeyId = staff.Where(p => p.S_account == name && p.S_password == pwd).FirstOrDefault().S_id;
                return Json<Common.LoginCommon>(login);
            }
        }
        [Route("api/Entrance/GetMenuShow")]
        public JsonResult<List<MenuInfo>> GetMenuShow(int id)
        {
            //显示所有员工信息
            List<Staff> staff = StaffViewModel.StaffSelect();
            //根据ID查看该员工拥有的角色的ID
            List<StaffAndRole> SARList = StaffAndRoleViewModel.StaffAndRoleSelect1(id);
            //查询RoleAndMenu表中的所有信息
            List<RoleAndMenu> RAMList=RoleAndMenuViewModel.RoleAndMenuSelect();
            //空的菜单表
            List<MenuInfo> MenuList = new List<MenuInfo>();
            List<MenuInfo> menulist=MenuInfoViewModel.MenuSelect();
            foreach (var SARListitem in SARList)
            {
                List<RoleAndMenu> RoleAndMenuList = RAMList.Where(p => p.RoleId == SARListitem.R_id).ToList();
                foreach (var RoleAndMenuListitem in RoleAndMenuList)
                {
                    MenuInfo menu = menulist.Where(p => p.MenuId == RoleAndMenuListitem.MenuId).FirstOrDefault();
                    MenuList.Add(menu);
                }
            }
            string LoginName = staff.Where(p => p.S_id == id).FirstOrDefault().S_account;
            return Json<List<MenuInfo>>(MenuList);
        }
        [Route("api/Entrance/GetMenuInfoSelectByParentID")]
        public JsonResult<List<MenuInfo>> GetMenuInfoSelectByParentID(int id)
        {
            List<MenuInfo> MenuByIDList = MenuInfoViewModel.MenuSelectByParentID(id);
            return Json<List<MenuInfo>>(MenuByIDList);
        }

        #endregion

        #region  房产勘察部分 API接口

        /// <summary>
        /// 根据用户登录ID查看需要勘察的信息
        /// </summary>
        /// <param name="id">登录用户ID</param>
        /// <returns></returns>
        [Route("api/Entrance/GetPersonalSelect")]
        public List<PersonalStatus> GetPersonalSelect(int id, int P_stateStart, int P_stateEnd)
        {
            var PersonalShow = PersonalViewModel.PersonalSelect(id, P_stateStart, P_stateEnd);
            return PersonalShow;
        }
        /// <summary>
        /// 根据ID修改登录信息当前状态
        /// </summary>
        /// <param name="id">修改信息主键ID</param>
        /// <returns></returns>
        [Route("api/Entrance/GetPersinalStatusUpd")]
        public bool GetPersinalStatusUpd(int id)
        {
            var PersonalStatus = PersonalViewModel.TrueOrFalse(id);
            return PersonalStatus;
        }


        /// <summary>
        /// 抵押物区位状况表添加
        /// </summary>
        /// <returns></returns>
        //[Route("api/Entrance/ZoneAdd")]
        public bool PostZoneAdd(Zone list)
        {

            /// <summary>
            /// 调用字典BLL中所有方法
            /// </summary>
            var ZoAdd = ZoneModel.ZoneAdd(list);
            return ZoAdd;
        }


        /// <summary>
        /// 图片表添加
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/SiteAdd")]
        public bool SiteAdd(string Picturename, string ptype, string Pictureurl)
        {
            /// <summary>
            /// 调用字典BLL中所有方法
            /// </summary>
            SitePicture list = new SitePicture();
            list.SP_Picturename = Picturename;
            list.SP_ptype = ptype;
            list.SP_Pictureurl = Pictureurl;
            var SiAdd = SitePictureModel.SiteAdd(list);
            return SiAdd;
        }

        /// <summary>
        /// 户内部分表添加
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/IndoorAdd")]
        public bool IndoorAdd(int purposeID, int storey, int orientationID, int residenceID, string house,
            string explain, decimal suggest, DateTime date, string personnel, int state, int drawingID,
            string roomdescribe, int habitableID, string habitable, int toiletID, string toilet, int kitchenID,
            string kitchen, string Thetotalfloor)
        {
            /// <summary>
            /// 调用字典BLL中所有方法
            /// </summary>
            Indoor list = new Indoor();
            list.I_purposeID = purposeID;
            list.I_storey = storey;
            list.I_orientationID = orientationID;
            list.I_residenceID = residenceID;
            list.I_house = house;
            list.I_explain = explain;
            list.I_suggest = suggest;
            list.I_date = date;
            list.I_personnel = personnel;
            list.I_state = state;
            list.I_drawingID = drawingID;
            list.I_roomdescribe = roomdescribe;
            list.I_habitableID = habitableID;
            list.I_habitable = habitable;
            list.I_toiletID = toiletID;
            list.I_toilet = toilet;
            list.I_kitchenID = kitchenID;
            list.I_kitchen = kitchen;
            list.I_Thetotalfloor = Thetotalfloor;
            var InAdd = IndoorModel.IndoorAdd(list);
            return InAdd;
        }

        /// <summary>
        /// 建筑公共部分表添加
        /// </summary>
        /// <returns></returns>
        //[Route("api/Entrance/FacirAdd")]
        public bool PostFacirAdd(Facility list)
        {
            /// <summary>
            /// 调用字典BLL中所有方法
            /// </summary>
            var FaAdd = FacilityModel.FaciAdd(list);
            return FaAdd;
        }

        /// <summary>
        /// 小区状况表添加
        /// </summary>
        /// <returns></returns>
        //[Route("api/Entrance/CommAdd")]
        public bool PostCommAdd(Community list)
        {
            /// <summary>
            /// 调用字典BLL中所有方法
            /// </summary>

            var CoAdd = CommunityModel.CommAdd(list);
            return CoAdd;
        }


        #endregion

        #region  房产评估部分 API接口

        #endregion

        #region  系统管理 API接口

        #region 角色调用方法
        /// <summary>
        /// 显示所有角色
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/GetRoleShow")]
        public List<Role> GetRoleShow()
        {
            var role = RoleViewModel.SelectRole();
            return role;
        }
        /// <summary>
        /// 角色查询（查询未删除）
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/GetRoleShow2")]
        public List<Role> GetRoleShow2(int id)
        {
            var role = RoleViewModel.RoleSelect2(id);
            return role;
        }
        /// <summary>
        /// 角色删除
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/GetRolefDelete")]
        public bool GetRolefDelete(int id)
        {
            List<Role> list = RoleViewModel.RoleSelect1(id);
            list[0].R_del = 1;
            Role r = list[0];
            var role = RoleViewModel.RoleUpdate(r);
            return role;
        }
        /// <summary>
        /// 角色添加
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/GetRoleAdd")]
        public bool GetRoleAdd(string RoleName, string RoleState)
        {
            Role list = new Role();
            list.R_name = RoleName;
            list.R_state = RoleState;
            list.R_time = DateTime.Now;
            list.R_del = 0;
            var role = RoleViewModel.RoleAdd(list);
            return role;
        }
        /// <summary>
        /// 角色查询（根据ID）
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/GetRoleShow1")]
        public List<Role> GetRoleShow1(int id)
        {
            var role = RoleViewModel.RoleSelect1(id);
            return role;
        }
        /// <summary>
        /// 角色修改
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/GetRoleUpdate")]
        public bool GetRoleUpdate(int id, string RoleName, string RoleState)
        {
            List<Role> list = RoleViewModel.RoleSelect1(id);
            list[0].R_id = id;
            list[0].R_name = RoleName;
            list[0].R_state = RoleState;
            Role r = list[0];          
            var role = RoleViewModel.RoleUpdate(r);
            return role;
        }
        #endregion
        #region 员工调用方法
        /// <summary>
        /// 显示所有员工
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/GetStaffShow")]
        public List<System_StaffState> GetStaff()
        {
            List<System_StaffState> Staff = StaffViewModel.StaffStateShow();
            return Staff;
        }
        /// <summary>
        ///左查询(查询未删除的账号)
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/GetShow")]
        public List<System_StafforRoleorDepartment> Getshow(int id)
        {

            var Staff = StaffViewModel.show(id);
            return Staff;

        }
        /// <summary>
        /// 根据登录员工ID查询拥有角色
        /// </summary>
        /// <param name="id">员工ID</param>
        /// <returns></returns>
        public List<Role> GetRoleNameByStaffID(int id)
        {
            List<Role> List = RoleViewModel.SelectRole();
            List<Role> RoleList = new List<Role>();

            //根据ID查看该员工拥有的角色的ID
            List<StaffAndRole> SARList = StaffAndRoleViewModel.StaffAndRoleSelect1(id);
            foreach (var item in SARList)
            {
                Role Role = List.Where(p => p.R_id == item.R_id).FirstOrDefault();
                RoleList.Add(Role);

            }
            return RoleList;
        }

        /// <summary>
        /// 员工查询（查询未删除ID）
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/GetStaffShow2")]
        public List<Staff> GetStaffShow2(int id)
        {
            var Staff = StaffViewModel.StaffSelect2(id);
            return Staff;
        }
        /// <summary>
        /// 员工删除
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/GetStaffDelete")]
        public bool GetStaffDelete(int id)
        {
            List<Staff> list = StaffViewModel.StaffSelect1(id);
            list[0].S_del = 1;
            Staff r = list[0];
            var Staff = StaffViewModel.StaffUpdate(r);
            return Staff;
        }
        /// <summary>
        /// 员工添加
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/GetStaffAdd")]
        public bool GetStaffA(string name,string account, string password, string usersex, string phone, string email,int Role,string S_IDcard)
        {
            Staff list = new Staff();
            list.S_name = name;
            list.S_phone =Convert.ToInt64( phone);
            list.S_sex = usersex;
            list.S_account = account;
            list.S_password = password;
            list.S_email = email;           
            list.S_status = 0;
           // list.R_id =Role;//员工角色   
            list.S_del=0;
            list.S_time = DateTime.Now;
            list.S_IDcard = S_IDcard;
            var Staff = StaffViewModel.StaffAdd(list);
            Staff li =StaffViewModel.StaffSelect3(list.S_IDcard);
            StaffAndRole l = new StaffAndRole();
            l.S_id = li.S_id;
            l.R_id = Role;
            StaffAndRoleViewModel.StaffAndRoleAdd(l);
            return Staff;
        }
        /// <summary>
        /// 员工查询（根据ID）
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/GetStaffShow1")]
        public List<Staff> GetStaffShow1(int id)
        {
            var Staff = StaffViewModel.StaffSelect1(id);
            return Staff;
        }
        /// <summary>
        /// 员工修改
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/GetStaffUpdate")]
        public bool GetStaffUpdate(int id, string name, string account, string password, string usersex, string phone, string email, int Role, string S_IDcard)      
        {
            List<Staff> list = StaffViewModel.StaffSelect1(id);          
             list[0].S_id = id;
             list[0].S_name = name;
             list[0].S_phone = Convert.ToInt64(phone);
             list[0].S_sex = usersex;
             list[0].S_account = account;
             list[0].S_password = password;
             list[0].S_email = email;
             list[0].S_IDcard = S_IDcard;
             Staff r = list[0];
             List<StaffAndRole> li = StaffAndRoleViewModel.StaffAndRoleSelect1(id);
             li[0].R_id = Role;
             StaffAndRole y = li[0];
             StaffAndRoleViewModel.StaffAndRoleUpdate(y);
            var Staff = StaffViewModel.StaffUpdate(r);

            return Staff;
        }
        /// <summary>
        /// 员工修改(停用)
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/GetStaffUpdate1")]
        public bool GetStaffUpdate1(int id)
        {
            List<Staff> list = StaffViewModel.StaffSelect1(id);
            list[0].S_status = 0;
            Staff r = list[0];                  
            var Staff = StaffViewModel.StaffUpdate(r);
            return Staff;
        }
        /// <summary>
        /// 员工修改(起用)
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/GetStaffUpdate2")]
        public bool GetStaffUpdate2(int id)
        {
            List<Staff> list = StaffViewModel.StaffSelect1(id);
            list[0].S_status = 1;
            Staff r = list[0];   
            var Staff = StaffViewModel.StaffUpdate(r);
            return Staff;
        }
        #endregion
        #region 部门调用方法


        /// <summary>
        /// 显示所有部门
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/GetDepartmentShow")]
        public List<Department> GetDepartmentShow()
        {
            var Depart = DepartmentViewModel.DepartmentSelect();
            return Depart;
        }

        /// <summary>
        /// 部门查询（查询未删除）
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/GetDepartmentShow2")]
        public List<System_DepartmentSelect> GetDepartmentShow2(int id)
        {
            var Depart = DepartmentViewModel.DepartmentSelect2(id);
            return Depart;
        }
        //public int GetListShow()
        //{
        //    List<Department> Depart = DepartmentViewModel.DepartmentSelect();
        //    return 1;
        //}
        /// <summary>
        /// 部门删除
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/GetDepartmentDelete")]
        public bool GetDepartmentDelete(int id)
        {
            List<Department> list = DepartmentViewModel.DepartmentSelect1(id);
            list[0].D_del = 1;
            Department r = list[0];
            var Depart = DepartmentViewModel.DepartmentUpdate(r);
            return Depart;
        }
        /// <summary>
        /// 部门添加
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/GetDepartmentAdd")]
        public bool GetDepartmentAdd(string name, string state, string phone, string home)
        {
            Department list = new Department();
            list.D_phone = phone;
            list.D_iid = Convert.ToInt32(phone.Substring(0, 4));
            list.D_name = name;
            list.D_state = state;
            list.D_status = 0;
            list.D_del = 0;
            list.D_time = DateTime.Now;
            list.D_home = home;

            var Depart = DepartmentViewModel.DepartmentAdd(list);
            return Depart;
        }
        /// <summary>
        /// 部门查询（根据ID）
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/GetDepartmentShow1")]
        public List<Department> GetDepartmentShow1(int id)
        {
            var Depart = DepartmentViewModel.DepartmentSelect1(id);
            return Depart;
        }
        /// <summary>
        /// 部门修改
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/DepartmentUpdate")]
        public bool GetDepartmentUpdate(int id, string name, string state, string phone, string home)
        {
            List<Department> list = DepartmentViewModel.DepartmentSelect1(id);
            list[0].D_phone = phone;
            list[0] .D_name = name;
            list[0] .D_state = state;
            list[0].D_home = home;
            Department r = list[0];
            var Depart = DepartmentViewModel.DepartmentUpdate(r);
            return Depart;
        }

        /// <summary>
        /// 部门修改（停用）
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/DepartmentUpdate1")]
        public bool GetDepartmentUpdate1(int id)
        {
            List<Department> list = DepartmentViewModel.DepartmentSelect1(id);
            list[0].D_status = 0;
            Department r = list[0];  
            var Depart = DepartmentViewModel.DepartmentUpdate(r);
            return Depart;
        }

        /// <summary>
        /// 部门修改（起用）
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/DepartmentUpdate2")]
        public bool GetDepartmentUpdate2(int id)
        {
            List<Department> list = DepartmentViewModel.DepartmentSelect1(id);
            list[0].D_status = 1;
            Department r = list[0];  
            var Depart = DepartmentViewModel.DepartmentUpdate(r);
            return Depart;
        }
        //public JsonResult<List<Department>> ListShow()
        //{
        //    List<Department> list = new List<Department>();
        //    return Json<List<Department>>(list);
        //}
        #endregion
        #endregion

        #region  通用部分 API接口

        #endregion

        #region  数据字典 API接口

        /// <summary>
        /// 字典类型信息显示
        /// </summary>
        [Route("api/Entrance/GetDictionary_StateShow")]
        public List<Dictionary_State> GetDictionary_StateShow()
        {
            var Dic_State = Dictionary_StateViewModel.Dicticonary_StateSelect();
            return Dic_State;
        }

        /// <summary>
        /// 字典信息显示
        /// </summary>
        [Route("api/Entrance/GetDictionary_link_show")]
        public List<System_DictionaryorDicState> GetDictionaryShow()
        {
            var DicShow = DictionaryViewModel.link_show();
            return DicShow;
        }

        /// <summary>
        /// 删除字典
        /// </summary>
        [Route("api/Entrance/GetDictionaryDelete")]
        public bool GetDictionaryDelete(int id)
        {
            var DicDel = DictionaryViewModel.DictionaryDelete(id);
            return DicDel;
        }

        /// <summary>
        /// 添加字典
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/GetDictionaryAdd")]
        public bool GetDictionaryAdd(string DictionaryName,int Did,int Dmenuid)
        {
            /// <summary>
            /// 调用字典BLL中所有方法
            /// </summary>
            Dictionary list = new Dictionary();
            list.Did = Did;
            list.DictionaryName = DictionaryName;
            list.Dmenuid = Dmenuid;
            var DicAdd = DictionaryViewModel.DictionaryAdd(list);
            return DicAdd;
        }

        /// <summary>
        /// 显示字典(根据Id查询)
        /// </summary>
        [Route("api/Entrance/GetDictionarySelect1")]
        public List<System_DictionaryorDicState> GetDictionarySelect1(int id)
        {
            /// <summary>
            /// 调用字典BLL中所有方法
            /// </summary>
            var DicShow1 = DictionaryViewModel.inquire_show(id);
            return DicShow1;
        }

        /// <summary>
        /// 显示字典(根据Id查询)
        /// </summary>
        [Route("api/Entrance/GetDictionary_single_show ")]
        public List<Dictionary> GetDictionary_single_show(int id)
        {
            /// <summary>
            /// 调用字典BLL中所有方法
            /// </summary>
            var DicShow1 = DictionaryViewModel.single_show(id);
            return DicShow1;
        }

        /// <summary>
        /// 修改信息字典
        /// </summary>
        /// <returns></returns>
        [Route("api/Entrance/GetDictionaryUpdate")]
        public bool GetDictionaryUpdate(int id, string DictionaryName,int Dmenuid)
        {
            /// <summary>
            /// 调用字典BLL中所有方法
            /// </summary>
            List<Dictionary> list = DictionaryViewModel.single_show(id);
            list[0].Did = id;
            list[0].DictionaryName = DictionaryName;
            list[0].Dmenuid = Dmenuid;
            Dictionary lis = list[0];
            var DicUpt = DictionaryViewModel.DictionaryUpdate(lis);
            return DicUpt;
        }

        #endregion

    }
}
